# -*- coding: utf-8 -*-
import traceback, os, re
import pycommonlib as pyclib
from pymongo    import  Connection
from termcolor  import  cprint

#server1 = '192.168.1.150'
#server2 = 'localhost'
server1 = 'localhost'
server2 = 'beta.mana.vn'
port1   = 27017
port2   = 27017
prefix  = '/home/hoangnamhai/HarvestedData'

conn1    = Connection(server1, port1)
conn2    = Connection(server2, port2)

db1      = conn1['mobile9']
db2      = conn2['my_database']

docs     = []

def uploadToServer(ssh, sftp, path_dir, path_file):
    try:
        # upload to server mana.vn
        result = False
        stdin, stdout, stderr   = ssh.exec_command("mkdir -p -m 0777 {0}".format(path_dir))
        remote_file = path_file
        info = pyclib.rexists(sftp, remote_file)
        if not info:
            for i in range(0, 3):
                try:
                    cprint(path_file, 'green')
                    sftp.put(path_file, remote_file)
                    print 'Copy file: ', path_file, ' => ', remote_file
                    result = True; break
                except:
                   continue 
        else:
            cprint('Đã tồn tại trên server', 'red'); result = True
        return result
    except:
        traceback.print_exc()

if __name__ == '__main__':
    try:
        cprint('start move data from local => server', 'yellow')
        collectionA = db1['article']
        collectionB = db2['article']

        ssh     = pyclib.createSSHClient('mana.vn', 22, 'daudm', '1p%^IRg')
        sftp    = ssh.open_sftp()
        if ssh==None: pyclib.forceQuit()

        result      = collectionA.find()
        for row in result:
            try:
                hashUrl = row['hashUrl']
                cexists = collectionB.find_one({'hashUrl': hashUrl, 'source': 'mobile9.com'})
                if cexists==None:
                    path_file = row['content']['download']['source']
                    path_file = path_file.replace('/uploads/', '')
                    subpath   = path_file[:15] 
                    path_file = '{0}/{1}'.format(prefix, path_file)
                    path_dir  = '{0}/{1}'.format(prefix, subpath)
                    game      = uploadToServer(ssh, sftp, path_dir, path_file)
                    cprint(game, 'yellow')
                    path_file = row['content']['image']['source']
                    path_file = path_file.replace('/uploads/', '')
                    subpath   = path_file[:15] 
                    path_file = '{0}/{1}'.format(prefix, path_file)
                    path_dir  = '{0}/{1}'.format(prefix, subpath)
                    image     = uploadToServer(ssh, sftp, path_dir, path_file)

                    if game==True and image==True:
                        collectionB.save(row)
                        cprint('Lưu thành công!', 'green')
                else: cprint('Đã tồn tại trong database.', 'red')
            except:
                traceback.print_exc()
                continue
        ssh.close()
        print 'Finished.'
    except:
        traceback.print_exc()

conn1.end_request()
conn2.end_request()
